package com.neuedu.servlet;


import com.google.gson.Gson;
import com.neuedu.pojo.PageResult;
import com.neuedu.pojo.Shop;
import com.neuedu.service.Imp.ShopServiceImp;
import com.neuedu.service.ShopService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getShop")
public class ShopServlet extends HttpServlet {
    private ShopService shopService = new ShopServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        switch (op){
            case "get":
                String shop = getShop(req, resp);
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().write(shop);
                break;
            case "gets":
                String shops = getsShop(req, resp);
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().write(shops);
                break;
            case "add":
                resp.getWriter().write(addShop(req, resp));
                break;
            case "update":
                resp.getWriter().write(updateShop(req, resp));
                break;
            case "updates":
                resp.getWriter().write(updateStatus(req, resp));
                break;
            case "del":
                resp.getWriter().write(delShop(req, resp));
                break;
            default:
                resp.getWriter().write("");
                break;
        }
    }


    // 添加数据的请求与响应
    public String addShop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{


        String shopName = req.getParameter("shopName");
        String shopAddress = req.getParameter("shopAddress");
        String shopExplain = req.getParameter("shopExplain");
        String shopImg = req.getParameter("shopImg");
        String shopCategoryId = req.getParameter("shopCategoryId");
        String startPrice = req.getParameter("startPrice");
        String deliveryPrice = req.getParameter("deliveryPrice");
        String remarks = req.getParameter("remarks");
        String status = req.getParameter("status");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String recommend = req.getParameter("recommend");
        String businessId = req.getParameter("businessId");

        Integer i = shopService.addShop(shopName,shopAddress,shopExplain,shopImg,shopCategoryId,startPrice,deliveryPrice,remarks,status,startTime,endTime,recommend,businessId);

        return i + "";
    }


    // 获取数据的请求与相应

    public String getShop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String businessId = req.getParameter("businessId");

        PageResult<List<Shop>> pageResult = shopService.getShop(currentPage,pageSize,businessId);

        return new Gson().toJson(pageResult);
    }

    // 条件查询
    public String getsShop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String shopName = req.getParameter("shopName");
        String status = req.getParameter("Status");


        PageResult<List<Shop>> pageResult = shopService.getsShop(currentPage,pageSize,shopName,status);

        return new Gson().toJson(pageResult);
    }

    public String updateShop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String shopName = req.getParameter("shopName");
        String shopAddress = req.getParameter("shopAddress");
        String shopExplain = req.getParameter("shopExplain");
        String shopImg = req.getParameter("shopImg");
        String shopCategoryId = req.getParameter("shopCategoryId");
        String startPrice = req.getParameter("startPrice");
        String deliveryPrice = req.getParameter("deliveryPrice");
        String remarks = req.getParameter("remarks");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String status = req.getParameter("status");
        String shopId = req.getParameter("shopId");

        Integer i = shopService.updateShop(shopName,shopAddress,shopExplain,shopImg,shopCategoryId,startPrice,deliveryPrice,remarks,startTime,endTime,status,shopId);
        return i + "";
    }

    public String updateStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String status = req.getParameter("status");
        String shopName = req.getParameter("shopName");

        Integer i = shopService.updateStatus(status,shopName);
        return i + "";
    }

    public String delShop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String shopId = req.getParameter("shopId");

        Integer i = shopService.delShop(shopId);
        return i + " ";
    }


}
